package com.culturalCenter.dataCenter.schedule.activity;

import cn.hutool.core.date.DateUtil;
import com.culturalCenter.dataCenter.Utils.DateFormatUtil;
import com.culturalCenter.dataCenter.Utils.RedisUtil;
import com.culturalCenter.dataCenter.Utils.TimeUtil;
import com.culturalCenter.dataCenter.entity.activity.*;
import com.culturalCenter.dataCenter.mapper.activity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 定时任务_活动_统计
 *
 * @Author zhao
 * @Date 2021-3-19 15:17
 */
@Component
@Slf4j
@Async("dataThreadExecutor")
public class ActivityDateStatistics {

    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private ActivityMonthCountMapper activityMonthCountMapper;
    @Resource
    private ActivityInfoCountMapper activityInfoCountMapper;
    @Resource
    private ActivityApplyMapper activityApplyMapper;
    @Resource
    private ActivityAudienceCountMapper activityAudienceCountMapper;
    @Resource
    private ActivityTimeIntervalMapper activityTimeIntervalMapper;
    @Resource
    private ActivityAuditRecordMapper activityAuditRecordMapper;
    @Resource
    private ActivityCreatorInfoMapper activityCreatorInfoMapper;
    @Resource
    private ActivitySignInMapper activitySignInMapper;
    @Resource
    private ActivityAttendanceMapper activityAttendanceMapper;
    @Resource
    private ActivityResourceCountMapper activityResourceCountMapper;
    @Resource
    private ActivitySiteMonitorMapper activitySiteMonitorMapper;
    @Resource
    private ActivityMonitorPersonnelMapper activityMonitorPersonnelMapper;
    @Resource
    private ActivityDepartmentStatisticsMapper activityDepartmentStatisticsMapper;
    @Resource
    private ActivityTodayMapper activityTodayMapper;
    @Resource
    private ActivityHostMapper activityHostMapper;
    @Resource
    private ActivityTypeCountMapper activityTypeCountMapper;
    @Resource
    private ActivityClientCountMapper activityClientCountMapper;
    @Resource
    private ActivityTodayCountMapper activityTodayCountMapper;
    @Resource
    private ActivityCommentCountMapper activityCommentCountMapper;
    @Resource
    private ActivityPushMapper activityPushMapper;
    @Resource
    private ActivityRoundMapper activityRoundMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private RedissonClient redisson;

    /**
     * for test
     */
    // @PostConstruct
    public void initLock() {
        for (int i = 1; i <= 23; i++) {
            redisUtil.del("activityStatistics_lock" + i);
        }
    }

    /**
     * 活动场次表分馆id更新（cdc_activity_round）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void updateBranchIdAround() {
        String lockKey = "activityStatistics_lock1";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_round分馆id开始统计更新");

            // 更新活动场次表分馆id
            List<ActivityRound> activityRoundList = activityRoundMapper.getActivityIdAndBranchId();
            if (CollectionUtils.isNotEmpty(activityRoundList)) {
                int size = 1000;
                int page = activityRoundList.size() % size == 0 ? activityRoundList.size() / size : activityRoundList.size() / size + 1;
                for (int i = 0; i < page; i++) {
                    activityRoundMapper.updateBranchIdBatch(activityRoundList.stream().skip(i * size).limit(size).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity_round分馆id更新统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 本月活动数统计（cdc_activity_month_count）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityMonthCount() {
        String lockKey = "activityStatistics_lock2";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("今日cdc_activity_month_count开始统计");

            List<ActivityMonthCount> countListToday = new ArrayList<>();
            List<String> branchIds = activityMonthCountMapper.getAllBranchIds();
            branchIds.forEach(branchId -> {
                for (Date beginDay = DateUtil.beginOfMonth(DateUtil.date()); beginDay.before(DateUtil.beginOfDay(DateUtil.tomorrow())); beginDay = DateUtil.offsetDay(beginDay, 1)) {
                    ActivityMonthCount activityMonthCount = activityMonthCountMapper.selectCountByBranchIdAndDay(beginDay, branchId);
                    activityMonthCount.setId(UUID.randomUUID().toString());
                    activityMonthCount.setBranchId(branchId);
                    activityMonthCount.setBeginTime(beginDay);
                    countListToday.add(activityMonthCount);
                }
            });
            activityMonthCountMapper.clearTable();
            activityMonthCountMapper.insertBatchOrUpdateToday(countListToday);

            log.info("今日cdc_activity_month_count统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 活动基本信息统计（cdc_activity_info_count）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityInfoCount() {
        String lockKey = "activityStatistics_lock3";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_info_count开始统计");

            log.info("*************开始更新关于活动基础表的信息******************");
            List<ActivityInfoCount> activityInfoCountList1 = activityInfoCountMapper.selectActivityInfo();
            if (CollectionUtils.isNotEmpty(activityInfoCountList1)) {
                for (ActivityInfoCount activityInfoCount : activityInfoCountList1) {
                    String id = UUID.randomUUID().toString();
                    activityInfoCount.setId(id);
                }
                int pages = activityInfoCountList1.size() % 5000 == 0 ? activityInfoCountList1.size() / 5000 : activityInfoCountList1.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityInfoCountMapper.insertOrUpdateActivityInfo(activityInfoCountList1.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }
            log.info("**************关于活动基础表的更新完毕*********************");

            log.info("**************开始更新用户活动表相关部分*******************");
            List<ActivityInfoCount> activityInfoCountList2 = activityInfoCountMapper.selectUserActivityInfo();
            if (CollectionUtils.isNotEmpty(activityInfoCountList2)) {
                for (ActivityInfoCount activityInfoCount : activityInfoCountList2) {
                    String id = UUID.randomUUID().toString();
                    activityInfoCount.setId(id);
                }
                int pages = activityInfoCountList2.size() % 5000 == 0 ? activityInfoCountList2.size() / 5000 : activityInfoCountList2.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityInfoCountMapper.insertOrUpdateUserActivityInfo(activityInfoCountList2.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }
            log.info("**************关于用户活动表部分更新完毕********************");

            log.info("cdc_activity_info_count统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 活动报名记录统计（cdc_activity_apply，只统计最新活动场次的报名情况）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityApply() {
        String lockKey = "activityStatistics_lock4";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_apply开始统计(只统计最新活动场次的报名情况)");

            activityApplyMapper.clearTable();
            List<ActivityApply> activityApplyList = activityApplyMapper.countActivitySignRecordsInfo();
            if (CollectionUtils.isNotEmpty(activityApplyList)) {
                for (ActivityApply activityApply : activityApplyList) {
                    String id = UUID.randomUUID().toString();
                    activityApply.setId(id);
                }
                int pages = activityApplyList.size() % 5000 == 0 ? activityApplyList.size() / 5000 : activityApplyList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityApplyMapper.insertBatch(activityApplyList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity_apply统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 活动受众分析统计（cdc_activity_audience_count）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityAudienceCount() {
        String lockKey = "activityStatistics_lock5";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_audience_count开始统计");

            activityAudienceCountMapper.clearTable();
            List<ActivityAudienceCount> activityAudienceCountList = activityAudienceCountMapper.countAudienceCount();
            if (CollectionUtils.isNotEmpty(activityAudienceCountList)) {
                for (ActivityAudienceCount activityAudienceCount : activityAudienceCountList) {
                    activityAudienceCount.setId(UUID.randomUUID().toString());
                }
                int pages = activityAudienceCountList.size() % 5000 == 0 ? activityAudienceCountList.size() / 5000 : activityAudienceCountList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityAudienceCountMapper.insertBatch(activityAudienceCountList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity_audience_count统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 各时间段活动参与人数统计（cdc_activity_time_interval）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityTimeInterval() {
        String lockKey = "activityStatistics_lock6";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_time_interval开始统计");

            List<ActivityTimeInterval> activityTimeIntervalList = activityTimeIntervalMapper.countTimeInterval();
            if (CollectionUtils.isNotEmpty(activityTimeIntervalList)) {
                for (ActivityTimeInterval activityTimeInterval : activityTimeIntervalList) {
                    String id = UUID.randomUUID().toString();
                    activityTimeInterval.setId(id);
                }
                activityTimeIntervalMapper.clearTable();
                activityTimeIntervalMapper.insertBatch(activityTimeIntervalList);
            }

            log.info("cdc_activity_time_interval统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 生成活动审核记录表（cdc_activity_audit_record（reviewer、reviewTime））
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityAuditRecord() {
        String lockKey = "activityStatistics_lock7";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_audit_record表开始统计");

            activityAuditRecordMapper.clearTable();

            List<ActivityAuditRecord> activityAuditRecordList = activityAuditRecordMapper.countAuditRecord();
            if (CollectionUtils.isNotEmpty(activityAuditRecordList)) {
                int pages = activityAuditRecordList.size() % 5000 == 0 ? activityAuditRecordList.size() / 5000 : activityAuditRecordList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityAuditRecordMapper.insertBatch(activityAuditRecordList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity_audit_record统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 生成活动创建记录表（cdc_activity_creator_info）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityCreatorInfo() {
        String lockKey = "activityStatistics_lock8";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_audit_record开始统计");

            List<ActivityCreatorInfo> activityCreatorInfoList = activityCreatorInfoMapper.countCreatorInfo();
            if (CollectionUtils.isNotEmpty(activityCreatorInfoList)) {
                int pages = activityCreatorInfoList.size() % 5000 == 0 ? activityCreatorInfoList.size() / 5000 : activityCreatorInfoList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityCreatorInfoMapper.insertBatch(activityCreatorInfoList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity_audit_record统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 生成用户活动签到清单（cdc_activity_signIn）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivitySignInInfo() {
        String lockKey = "activityStatistics_lock9";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_signIn开始统计");

            activitySignInMapper.clearTable();
            List<ActivitySignIn> activitySignInList = activitySignInMapper.countSignInIn();
            if (CollectionUtils.isNotEmpty(activitySignInList)) {
                for (ActivitySignIn activitySignIn : activitySignInList) {
                    String id = UUID.randomUUID().toString();
                    activitySignIn.setId(id);
                }
                int pages = activitySignInList.size() % 5000 == 0 ? activitySignInList.size() / 5000 : activitySignInList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activitySignInMapper.insertBatch(activitySignInList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity_signIn统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 生成活动考勤清单（cdc_activity_attendance）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityAttendanceInfo() {
        String lockKey = "activityStatistics_lock10";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_attendance开始统计");

            activityAttendanceMapper.clearTable();
            List<ActivityAttendance> activityAttendanceList = activityAttendanceMapper.countAttendance();
            if (CollectionUtils.isNotEmpty(activityAttendanceList)) {
                for (ActivityAttendance activityAttendance : activityAttendanceList) {
                    String id = UUID.randomUUID().toString();
                    activityAttendance.setId(id);
                }
                int pages = activityAttendanceList.size() % 5000 == 0 ? activityAttendanceList.size() / 5000 : activityAttendanceList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityAttendanceMapper.insertBatch(activityAttendanceList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity_attendance统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 统计图片视频（cdc_activity_resource_count）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityResourceCount() {
        String lockKey = "activityStatistics_lock11";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_resource_count开始统计");

            activityResourceCountMapper.clearTable();
            List<ActivityResourceCount> activityResourceCountList = activityResourceCountMapper.countResourceCount();
            if (CollectionUtils.isNotEmpty(activityResourceCountList)) {
                for (ActivityResourceCount activityResourceCount : activityResourceCountList) {
                    String id = UUID.randomUUID().toString();
                    activityResourceCount.setId(id);
                }
                activityResourceCountMapper.insertBatch(activityResourceCountList);
            }

            log.info("cdc_activity_resource_count统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 生成活动现场监控记录（cdc_activity_site_monitor）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivitySiteMonitor() {
        String lockKey = "activityStatistics_lock12";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_site_monitor开始统计");

            activitySiteMonitorMapper.clearTable();
            List<ActivitySiteMonitor> activitySiteMonitorList = activitySiteMonitorMapper.countSiteMonitor();
            if (CollectionUtils.isNotEmpty(activitySiteMonitorList)) {
                for (ActivitySiteMonitor activitySiteMonitor : activitySiteMonitorList) {
                    String id = UUID.randomUUID().toString();
                    activitySiteMonitor.setId(id);
                }
                activitySiteMonitorMapper.insertBatch(activitySiteMonitorList);
            }

            log.info("cdc_activity_site_monitor统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 生成现场人员监控记录（cdc_activity_monitor_personnel）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityMonitorPersonnel() {
        String lockKey = "activityStatistics_lock13";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_monitor_personnel开始统计");

            activityMonitorPersonnelMapper.clearTable();
            List<ActivityMonitorPersonnel> monitorPersonnelList = activityMonitorPersonnelMapper.countMonitorPersonne();
            if (CollectionUtils.isNotEmpty(monitorPersonnelList)) {
                for (ActivityMonitorPersonnel activityMonitorPersonnel : monitorPersonnelList) {
                    activityMonitorPersonnel.setId(UUID.randomUUID().toString());
                }
                activityMonitorPersonnelMapper.insertBatch(monitorPersonnelList);
            }

            log.info("cdc_activity_monitor_personnel统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 部门活动统计（cdc_activity_department_statistics）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityDepartmentStatistics() {
        String lockKey = "activityStatistics_lock14";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_department_statistics开始统计");

            activityDepartmentStatisticsMapper.clearTable();
            List<ActivityDepartmentStatistics> activityDepartmentStatisticsList = activityDepartmentStatisticsMapper.countDepartmentActivitiesNum(DateFormatUtil.formatMonth(new Date()));
            if (CollectionUtils.isNotEmpty(activityDepartmentStatisticsList)) {
                for (ActivityDepartmentStatistics activityDepartmentStatistics : activityDepartmentStatisticsList) {
                    String id = UUID.randomUUID().toString();
                    activityDepartmentStatistics.setId(id);
                }
                activityDepartmentStatisticsMapper.updateActivitiesNum(activityDepartmentStatisticsList);
            }

            log.info("cdc_activity_department_statistics统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 今日活动（cdc_activity_today）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityToday() {
        String lockKey = "activityStatistics_lock15";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_today开始统计");

            activityTodayMapper.clearTable();
            log.info("*******************统计今日活动表主体内容*********************");
            List<ActivityToday> activityTodayList = activityTodayMapper.countActivityToday(DateFormatUtil.format(new Date()));
            if (CollectionUtils.isNotEmpty(activityTodayList)) {
                for (ActivityToday activityToday : activityTodayList) {
                    String id = UUID.randomUUID().toString();
                    activityToday.setId(id);
                }
                activityTodayMapper.insertOrUpdateActivityToday(activityTodayList);
            }

            log.info("*******************统计今日活动表报名人数*********************");
            List<ActivityToday> activityTodayList1 = activityTodayMapper.countActivityTodayRegisterNum(DateFormatUtil.format(new Date()));
            if (CollectionUtils.isNotEmpty(activityTodayList1)) {
                for (ActivityToday activityToday : activityTodayList1) {
                    String id = UUID.randomUUID().toString();
                    activityToday.setId(id);
                }
                activityTodayMapper.insertOrUpdateRegisterNum(activityTodayList1);
            }

            log.info("cdc_activity_today统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 本月热门活动（cdc_activity_host）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityHost() {
        String lockKey = "activityStatistics_lock16";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_host开始统计");

            activityHostMapper.clearTable();
            List<ActivityHost> activityHostList = activityHostMapper.countHost(DateFormatUtil.formatMonth(new Date()));
            if (CollectionUtils.isNotEmpty(activityHostList)) {
                for (ActivityHost activityHost : activityHostList) {
                    String id = UUID.randomUUID().toString();
                    activityHost.setId(id);
                }
                activityHostMapper.insertBatch(activityHostList);
            }

            log.info("cdc_activity_host统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 分类报名人数统计（cdc_activity_type_count）
     */
//    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityTypeCount() {
        String lockKey = "activityStatistics_lock17";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_type_count开始统计");

            activityTypeCountMapper.clearTable();
            List<ActivityTypeCount> activityTypeCountList = activityTypeCountMapper.countTypeCount(DateFormatUtil.formatMonth(new Date()), TimeUtil.getLastMonthYearAndMonth());
            if (CollectionUtils.isNotEmpty(activityTypeCountList)) {
                for (ActivityTypeCount activityTypeCount : activityTypeCountList) {
                    String id = UUID.randomUUID().toString();
                    activityTypeCount.setId(id);
                }
                activityTypeCountMapper.insertBatch(activityTypeCountList);
            }

            log.info("cdc_activity_type_count统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 访问者客户端类型统计（cdc_activity_client_count）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityClientCount() {
        String lockKey = "activityStatistics_lock18";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_client_count开始统计");

            activityClientCountMapper.clearTable();
            List<ActivityClientCount> activityClientCountList = activityClientCountMapper.countClientCount();
            if (CollectionUtils.isNotEmpty(activityClientCountList)) {
                for (ActivityClientCount activityClientCount : activityClientCountList) {
                    String id = UUID.randomUUID().toString();
                    activityClientCount.setId(id);
                }
                activityClientCountMapper.insertBatch(activityClientCountList);
            }

            log.info("cdc_activity_client_count统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 今日活动数量统计（cdc_activity_today_count）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityTodayCount() {
        String lockKey = "activityStatistics_lock19";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_today_count开始统计");

            activityTodayCountMapper.clearTable();
            List<ActivityTodayCount> activityTodayCountList = activityTodayCountMapper.countTodayCount(DateFormatUtil.formatSecond(new Date()));
            if (CollectionUtils.isNotEmpty(activityTodayCountList)) {
                for (ActivityTodayCount activityTodayCount : activityTodayCountList) {
                    activityTodayCount.setId(UUID.randomUUID().toString());
                }
                activityTodayCountMapper.insertBatch(activityTodayCountList);
            }

            log.info("cdc_activity_today_count统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 更新活动状态（cdc_activity的status）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityStatus() {
        String lockKey = "activityStatistics_lock20";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("开始统计更新cdc_activity活动状态");

            List<Activity> activityList = activityMapper.selectAllIds();
            if (CollectionUtils.isNotEmpty(activityList)) {
                activityList.forEach(activity -> activity.setStatus(activityMapper.countActivityStatus(activity.getId())));
                int pages = activityList.size() % 5000 == 0 ? activityList.size() / 5000 : activityList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityMapper.insertOrUpdateStatus(activityList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity活动状态更新统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 更新活动的开始、结束时间（cdc_activity的beginTime、endTime）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityTime() {
        String lockKey = "activityStatistics_lock21";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("开始统计cdc_activity的beginTime、endTime");

            List<Activity> activityList = activityMapper.countActivityTime();
            if (CollectionUtils.isNotEmpty(activityList)) {
                int pages = activityList.size() % 5000 == 0 ? activityList.size() / 5000 : activityList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityMapper.insertBatchActivityTime(activityList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity的beginTime、endTime统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 活动评论信息统计（cdc_activity_comment_count）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityCommentCount() {
        String lockKey = "activityStatistics_lock22";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_comment_count开始统计");

            List<ActivityCommentCount> activityCommentCountList = activityCommentCountMapper.countCommentPart();
            if (CollectionUtils.isNotEmpty(activityCommentCountList)) {
                for (ActivityCommentCount activityCommentCount : activityCommentCountList) {
                    String id = UUID.randomUUID().toString();
                    activityCommentCount.setId(id);
                }
                int pages = activityCommentCountList.size() % 5000 == 0 ? activityCommentCountList.size() / 5000 : activityCommentCountList.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityCommentCountMapper.insertOrUpdateCommentPart(activityCommentCountList.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            List<ActivityCommentCount> activityCommentCountList1 = activityCommentCountMapper.countActivityPart();
            if (CollectionUtils.isNotEmpty(activityCommentCountList1)) {
                for (ActivityCommentCount activityCommentCount : activityCommentCountList1) {
                    String id = UUID.randomUUID().toString();
                    activityCommentCount.setId(id);
                }
                int pages = activityCommentCountList1.size() % 5000 == 0 ? activityCommentCountList1.size() / 5000 : activityCommentCountList1.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityCommentCountMapper.insertOrUpdateActivityPart(activityCommentCountList1.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            List<ActivityCommentCount> activityCommentCountList2 = activityCommentCountMapper.countHighPart();
            if (CollectionUtils.isNotEmpty(activityCommentCountList2)) {
                for (ActivityCommentCount activityCommentCount : activityCommentCountList2) {
                    String id = UUID.randomUUID().toString();
                    activityCommentCount.setId(id);
                }
                int pages = activityCommentCountList2.size() % 5000 == 0 ? activityCommentCountList2.size() / 5000 : activityCommentCountList2.size() / 5000 + 1;
                for (int i = 0; i < pages; i++) {
                    activityCommentCountMapper.insertOrUpdateHighPart(activityCommentCountList2.stream().skip(i * 5000).limit(5000).collect(Collectors.toList()));
                }
            }

            log.info("cdc_activity_comment_count统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

    /**
     * 今日推荐活动（cdc_activity_push）
     */
    //@Scheduled(cron = "${cron.activityStatistics}")
    // @Scheduled(cron = "${cron.test}")
    public void statisticsActivityTodayPush() {
        String lockKey = "activityStatistics_lock23";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            boolean locked = redissonLock.isLocked();
            if (!locked){
                redissonLock.lock();
            }
            log.info("cdc_activity_push开始统计");

            activityPushMapper.clearTable();
            List<ActivityPush> activityPushList = activityPushMapper.countTodayPush();
            if (CollectionUtils.isNotEmpty(activityPushList)) {
                for (ActivityPush activityPush : activityPushList) {
                    String id = UUID.randomUUID().toString();
                    activityPush.setId(id);
                }
                activityPushMapper.insertBatch(activityPushList);
            }

            log.info("cdc_activity_push统计完毕");
        } finally {
            boolean locked = redissonLock.isLocked();
            if (locked){
                redissonLock.unlock();
            }
        }
    }

}
